package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.game.RoleLifeSkill;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPalette;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;

public class UILifeSkillsWindow extends UIAbstractWindow
{

	public UILifeSkillsWindow(int id)
	{
		super(id);
		initImage();
	}
	
	private void initImage() {
//		img_title_small = UIManager.getInstance().createImage(UIManager.RES_TITLE_SMALL);
//		img_title_middle = UIManager.getInstance().createImage(UIManager.RES_TITLE_MIDDLE);
//		img_select_corner = UIManager.getInstance().createImage(UIManager.RES_SELECT_CORNER);
//		img_scroll_bar_arrows = UIManager.getInstance().createImage(UIManager.RES_SCROLL_BAR_ARROWS);
//		img_scroll_bar_ruling = UIManager.getInstance().createImage(UIManager.RES_SCROLL_BAR_RULING);
//		img_border_corner = UIManager.getInstance().createImage(UIManager.RES_BORDER_CORNER);
//		img_softkey = UIManager.getInstance().createImage(UIManager.RES_BUTTON_LEFT);
	}

	protected void paintContent(Graphics g)
	{
		switch (m_state) {
			case STATUS_STUDY_LS_LIST:
				drawStudyLSList(g);
				break;
		}
	}
	/**
	 * 绘制学习生活技能列表
	 * @param g
	 */
	private void drawStudyLSList(Graphics g) {
		UITools.clearScreen(g);//清屏
		UITools.drawWindowBackgroud(g);//绘制背景
		UITools.drawWindowSubcoat(g,
				UITools.X_SUBCOAT_HT,
				UITools.Y_SUBCOAT_HT,
				UITools.W_SUBCOAT_HT,
				UITools.H_SUBCOAT_HT);//绘制内层背景
		drawStudyLSListTitle(g);//绘制按钮标题
		drawStudyLSListContent(g);//绘制内容
		UITools.drawScrollBar(g,
				UITools.X_SCROLL_BAR,
				UITools.Y_SCROLL_BAR,
				UITools.H_SCROLL_BAR,
				-1);//绘制滚动条
		UITools.drawWindowSoftkey(g, "操作", "返回");//绘制左右软件
		UITools.drawTitle(g, "学习生活技能");//绘制标题
	}
	/**
	 * 绘制学习生活技能列表内容
	 * @param g
	 */
	private void drawStudyLSListContent(Graphics g) {
		for (int i = 0; i < UITools.AMOUNT_SUBCOAT_HT_CONTENT; i++) {
			drawContentBox(g, UITools.X_SUBCOAT_HT, UITools.Y_SUBCOAT_HT,
					UITools.W_SUBCOAT_HT, AbstractGameScreen.FONT_H + UITools.GAP_EACH_LINE, i);
		}
	}
	
	/**
	 * 绘制内层单框
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param index
	 * 当前页面的索引
	 */
	private void drawContentBox(Graphics g, int x, int y, int w, int h, int index)
	{
		if (m_study_list != null) {
			RoleLifeSkill ls = null;
			int wordColor = UIManager.getPaletteColor(UIPalette.COLOR_WORD_NOS);
			if (index == m_study_list_index) {
				g.setColor(UIManager.getPaletteColor(UIPalette.COLOR_CBD_SELECT));
				wordColor = UIManager.getPaletteColor(UIPalette.COLOR_WORD_YESS);
			} else if ((m_study_list_page + index) % 2 == 0) {// 单数颜色
				g.setColor(UIManager.getPaletteColor(UIPalette.COLOR_CBD_DEEP));
			} else {
				g.setColor(UIManager.getPaletteColor(UIPalette.COLOR_CBD_LIGHT));
			}
			int len = UIManager.getColorsSubcoatBorder().length;
			g.fillRect(x + len, y + len + index * h, w - (len << 1), h);
			
			if (m_study_list_page + index < m_study_list.size()) {
				ls = (RoleLifeSkill) m_study_list.elementAt(m_study_list_page + index);
				ls.drawStudyContent(g, x + 6, y + 6 + index * (AbstractGameScreen.FONT_H + UITools.GAP_EACH_LINE), wordColor);
			}
		}
	}
	/**
	 * 绘制学习生活技能列表标题
	 * @param g
	 */
	private void drawStudyLSListTitle(Graphics g)
	{
		for (int i = 0, x = 0; i < STUDY_LS_STATUS_AMOUNT; i++) {
			x = UITools.X_BUTTON_4MID + UITools.W_BUTTON_MIDDLE * i;
			g.setClip(x, UITools.Y_BUTTON, UITools.W_BUTTON_MIDDLE, UITools.H_BUTTON);
			if (i == m_study_status) {
//				g.drawImage(img_button_middle[BUTTON_MIDDLE_YES], x, UITools.Y_BUTTON, Tools.LEFT_TOP);
				
				UITools.paintLabel(g, x, UITools.Y_BUTTON, UITools.W_LABEL_MIDDLE, UITools.H_LABEL, true) ;
				UITools.drawImageSelectBox(g, x,
						UITools.Y_BUTTON, UITools.W_BUTTON_MIDDLE, UITools.H_BUTTON);
				
			}
			else {
//				g.drawImage(img_button_middle[BUTTON_MIDDLE_NO], x, UITools.Y_BUTTON, Tools.LEFT_TOP);
				UITools.paintLabel(g, x, UITools.Y_BUTTON, UITools.W_LABEL_MIDDLE, UITools.H_LABEL, false) ;
			}
			switch (i) {
				case STUDY_LS_STATUS_FLORA:
					UITools.drawColorString(g, "草木", x + (UITools.W_BUTTON_MIDDLE >> 1),
							UITools.Y_BUTTON + (UITools.H_BUTTON >> 1) - (AbstractGameScreen.FONT_H >> 1),
							UIManager.getPaletteColor(UIPalette.COLOR_WORD_NOS), Tools.HCENTER_TOP);
					break;
				case STUDY_LS_STATUS_STONE:
					UITools.drawColorString(g, "金石", x + (UITools.W_BUTTON_MIDDLE >> 1),
							UITools.Y_BUTTON + (UITools.H_BUTTON >> 1) - (AbstractGameScreen.FONT_H >> 1),
							UIManager.getPaletteColor(UIPalette.COLOR_WORD_NOS), Tools.HCENTER_TOP);
					break;
				case STUDY_LS_STATUS_ALCHEMY:
					UITools.drawColorString(g, "炼金", x + (UITools.W_BUTTON_MIDDLE >> 1),
							UITools.Y_BUTTON + (UITools.H_BUTTON >> 1) - (AbstractGameScreen.FONT_H >> 1),
							UIManager.getPaletteColor(UIPalette.COLOR_WORD_NOS), Tools.HCENTER_TOP);
					break;
				case STUDY_LS_STATUS_ARTIFICE:
					UITools.drawColorString(g, "巧匠", x + (UITools.W_BUTTON_MIDDLE >> 1),
							UITools.Y_BUTTON + (UITools.H_BUTTON >> 1) - (AbstractGameScreen.FONT_H >> 1),
							UIManager.getPaletteColor(UIPalette.COLOR_WORD_NOS), Tools.HCENTER_TOP);
					break;
			}
		}
	}
	protected void keyLogic()
	{
		switch (m_state) {
			case STATUS_STUDY_LS_LIST:
				logicStudyLSList();
				break;
		}
	}
	private void logicStudyLSList() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
			m_study_status = --m_study_status < 0 ? STUDY_LS_STATUS_AMOUNT - 1 : m_study_status;
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			m_study_status = ++m_study_status >= STUDY_LS_STATUS_AMOUNT ? 0 : m_study_status;
		}
	}
	/******************* 网络接收 *********************/
	/**
	 * 读取NPC学习生活技能列表
	 * @param dis
	 * @throws IOException
	 */
	public void recvLSStudyList(DataInputStream dis) throws IOException
	{
		short state = dis.readShort();
		String msg = dis.readUTF();
		if (state == 1) {
			String name = dis.readUTF();// 商店名称
			Tools.debugPrintln("商店名称 : " + name);
			byte size = dis.readByte();// 技能类型长度
			Tools.debugPrintln("技能类型长度 : " + size);
			m_study_list = null;
			if (size > 0) {
				m_study_list = new ArrayList();
				for (int i = 0, j = 0; i < size; i++) {
					byte type = dis.readByte();// 技能类型
					Tools.debugPrintln("技能类型 : " + type);
					byte amount = dis.readByte();// 技能数量
					Tools.debugPrintln("技能数量 : " + amount);
					RoleLifeSkill[] lifeSkills = new RoleLifeSkill[amount];
					for (j = 0; j < amount; j++) {
						lifeSkills[j] = new RoleLifeSkill();
						lifeSkills[j].m_type = type;
						Tools.debugPrintln("技能" + i + " type = " + type);
						lifeSkills[j].m_key = dis.readUTF();// 技能ID
						Tools.debugPrintln("技能" + i + " key = " + lifeSkills[j].m_key);
						lifeSkills[j].m_name = dis.readUTF();// 技能名称
						Tools.debugPrintln("技能" + i + " name = " + lifeSkills[j].m_name);
						lifeSkills[j].m_price = dis.readLong();// 技能价格
						Tools.debugPrintln("技能" + i + " price = " + lifeSkills[j].m_price);
						lifeSkills[j].m_icon = dis.readInt();// 技能图标
						Tools.debugPrintln("技能" + i + " icon = " + lifeSkills[j].m_icon);
						lifeSkills[j].m_status = dis.readByte();// 技能状态,0-已学,1-可学,2-未达到要求
						Tools.debugPrintln("技能" + i + " status = " + lifeSkills[j].m_status);
					}
					if (amount > 0)
						m_study_list.addElement(lifeSkills);
				}
			}
			setWindowStatus(STATUS_STUDY_LS_LIST);
			UIManager.getInstance().addWindow(this);
		}
		else {
			Tools.debugPrintln("读取NPC学习生活技能异常 : " + msg);
		}
	}
	
	private final byte STUDY_LS_STATUS_FLORA = 0;// 草木
	private final byte STUDY_LS_STATUS_STONE = 1;// 金石
	private final byte STUDY_LS_STATUS_ALCHEMY = 2;// 炼金
	private final byte STUDY_LS_STATUS_ARTIFICE = 3;// 巧匠
	private final byte STUDY_LS_STATUS_AMOUNT = 4;
	
	private byte m_study_status = 0;
	private final int STATUS_STUDY_LS_LIST = 1;//学习生活技能
	/******************* 图片 *********************/
//	private Image img_title_small = null;// 窗口标题栏
//	private Image img_title_middle = null;// 窗口标题中间块
//	private Image img_select_corner = null;// 选择按钮的角
//	private Image img_scroll_bar_arrows = null;// 滚动条箭头
//	private Image img_scroll_bar_ruling = null;// 滚动条中间刻度
//	private Image img_border_corner = null;// 外框角

//	private Image img_softkey = null;// 左右软键按钮
	
//	private final int BUTTON_MIDDLE_YES = 0;// 按钮被按下
//	private final int BUTTON_MIDDLE_NO = 1;// 按钮没有被按下
//	private final int BUTTON_MIDDLE_COUNT = 2;// 按钮总数
	
	private ArrayList m_study_list = null;//学习生活技能列表
	private int m_study_list_page = 0;
	private int m_study_list_index = 0;
}
